<%@page import="java.lang.reflect.InvocationTargetException"%>
<%@page import="org.apache.commons.beanutils.BeanUtils"%>
<%@page import="drukarnia.controller.OrderManager"%>
<%@page import="drukarnia.model.Orders"%>
<%@page import="drukarnia.model.User"%>
<%@page import="java.util.Date"%>
<%@page import="java.util.Properties"%>
<%@ page errorPage="WEB-INF/bledy.jsp"%>
<%@page import="javax.mail.*" %>
<%@page import="javax.mail.internet.*" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>
<body>
	<%
	User user = (User)session.getAttribute("user");
	if(user != null){
		if(user.getAuthorization() == 'a')
		{
	
		String id = request.getParameter("id_order");
		int id_order = Integer.parseInt(id);	
		
		OrderManager userm = new OrderManager();
		userm.finished(id_order);
		/////////////////////////////
		String fileName = request.getParameter("fileName");
		String cost = request.getParameter("cost");
		String addressFrom = "jaclaw90@gmail.com";
	   // String addressTo   = user.getMail();
	    String addressTo   = request.getParameter("mail");
	    String mailSubject = "Zmiana statusu zlecenia";
	    String mailBody    = 
	    					"Status: zlecenie zakonczone powodzeniem \n" +
	    					
		    				"Pozdrawiamy, \n" +
		    				"DRUKARNIA"; 
		
	    try
	    {
	        
	        Properties props = new Properties();
	        props.put("mail.smtp.host", "smtp.gmail.com");
	        props.put("mail.smtp.socketFactory.port", "465");
	        props.put("mail.smtp.socketFactory.class",
	                        "javax.net.ssl.SSLSocketFactory");
	        props.put("mail.smtp.auth", "true");
	        props.put("mail.smtp.port", "465");
	        		
	        Session mailSession = Session.getInstance(props,
	                new javax.mail.Authenticator() {
	                        protected PasswordAuthentication getPasswordAuthentication() {
	                                return new PasswordAuthentication("io.hotelmanagement","projekthotel");
	                        }
	                });       
	                    
	        Message msg = new MimeMessage(mailSession);
	        msg.setFrom(new InternetAddress(addressFrom)); //nadawca
	        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(addressTo)); //odbiorca  
	        msg.setSubject(mailSubject); //temat
	        msg.setText(mailBody); 
	        msg.setSentDate(new Date());
	        
	                
	        
	        Transport.send(msg);
	       
	    }
	    catch (Exception ex)
	    {
	        out.println("Wystąpiły błędy: ");
	        out.println(ex);
	    }
		/////////////////////////////
		%>
			<jsp:forward page="orderHistoryADMIN.jsp">
			<jsp:param value="Zmiana statusu: zrealizowano, wyslano maila z potwierdzeniem" name="message"/>
			</jsp:forward>
		<%
		
		}
		}
		else{
			
			response.sendError(404,"Gladiator: brak uprawnien");
			}
	%>
	

</body>
</html>